package com.iyoudoock.citymine.activity.order;

import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewAnimationUtils;
import android.webkit.JavascriptInterface;

import com.iyoudoock.citymine.BaseActivity;
import com.iyoudoock.citymine.R;
import com.iyoudoock.citymine.activity.user.PersonCenterActivity;
import com.iyoudoock.citymine.constants.HttpUtils;
import com.iyoudoock.citymine.constants.Key;
import com.iyoudoock.citymine.constants.Value;
import com.kvin.toolkit.utils.JumpUtils;
import com.kvin.toolkit.utils.http.JsonUtils;
import com.kvin.toolkit.utils.system.AnimUtils;
import com.kvin.toolkit.widget.KvinWebView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UpdatePriceActivity extends BaseActivity {

    @BindView(R.id.m_web)
    KvinWebView mWeb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_price);
        ButterKnife.bind(this);
    }

    @Override
    public void initNav() {
        super.initNav();
        setTitle("更新价格");
    }

    @Override
    public void init() {
        mJsonUtils = JsonUtils.getInstance();

        mWeb.setVerticalScrollBarEnabled(false);
        mWeb.addJavascriptInterface(new JSInterface(this), "WebInterface");

        loadingData();
    }

    @Override
    public void loadingData() {
        String recyclerNo = mPersistentUtils.readString(Key.User.RECYCLER_NO);
        params = HttpUtils.categoryQuery(recyclerNo, "", "");
        mJsonUtils.post(HttpUtils.CATEGORY_QUERY_URL, params, this, Value.Flag.CATEGORY_QUERY);
    }


    @Override
    public void onSuccess(JSONObject response, String flag) {
        super.onSuccess(response, flag);
        if (response == null) return;
        try {
            int state = response.getInt(Key.Tag.STATE);
            if (flag.equals(Value.Flag.CATEGORY_QUERY)) {
                if (state == Value.Response.SUCCESS) {
                    mWeb.loadUrl(HttpUtils.CATEGORY_DETAIL_URL + mUid);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(IOException e, String flag) {
        super.onFailure(e, flag);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, Value.MenuItemId.SUBMIT, 0, "提交").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == Value.MenuItemId.SUBMIT) {
            mWeb.loadUrl("javascript:showConfirm()");
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * provide method for js
     */
    private final class JSInterface {
        private Context mcontext;

        JSInterface(Context context) {
            this.mcontext = context;
        }

        @JavascriptInterface
        public void submitCategoryFinish() {
            mHandler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    finish();
                    AnimUtils.setAnim(getActivity(), AnimUtils.getDefaultBackAnim());
                }
            }, 200);

        }

    }
}
